/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.AbstractLinkEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.SeDiSubpartEditPart;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the color section of uml elements.
 */
public class FigureColorsSection extends com.cea.papyrus.tabbedproperties.section.FigureColorsSection {


    /**
     * Creates the controls.
     * 
     * @param tabbedPropertySheetPage
     *            the tabbed property sheet page
     * @param parent
     *            the parent
     * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
     *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);

        // Replace listeners
        backgroundColorSelector.removeListener(backgroundColorButtonListener);
        borderColorSelector.removeListener(borderColorButtonListener);
        fontColorSelector.removeListener(fontColorButtonListener);
          
        //
        backgroundColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setBackgroundColor(
                                backgroundColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setBackgroundColor(
                                backgroundColorSelector.getColorValue());
                    } else if (graphicalElement instanceof SeDiSubpartEditPart
                            || graphicalElement instanceof AbstractLinkEditPart) {
                        if (graphicalElement.getModel() instanceof GraphElement) {
                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                            graphElement
                                    .setBackgroundColor(backgroundColorSelector.getColorValue());
                        }
                    }
                }
            }
        };
        backgroundColorSelector.addListener(backgroundColorButtonListener);

        borderColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setBorderColor(
                                borderColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setBorderColor(
                                borderColorSelector.getColorValue());
                    } else if (graphicalElement instanceof SeDiSubpartEditPart
                            || graphicalElement instanceof AbstractLinkEditPart) {
                        if (graphicalElement.getModel() instanceof GraphElement) {
                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                            graphElement.setBorderColor(borderColorSelector.getColorValue());
                        }
                    }
                }
            }
        };
        borderColorSelector.addListener(borderColorButtonListener);

        fontColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setFontColor(
                                fontColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontColor(
                                fontColorSelector.getColorValue());
                    } else if (graphicalElement instanceof SeDiSubpartEditPart
                            || graphicalElement instanceof AbstractLinkEditPart) {
                        if (graphicalElement.getModel() instanceof GraphElement) {
                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                            graphElement.setFontColor(fontColorSelector.getColorValue());
                        }
                    }
                }
            }
        };
        fontColorSelector.addListener(fontColorButtonListener);
    }

    /**
     * Refresh.
     */
    @Override
    public void refresh() {
        if (!backgroundColorSelector.getButton().isDisposed()
                && !borderColorSelector.getButton().isDisposed()
                && !fontColorSelector.getButton().isDisposed()) {

            // Remove all listeners
            backgroundColorSelector.removeListener(backgroundColorButtonListener);
            borderColorSelector.removeListener(borderColorButtonListener);
            fontColorSelector.removeListener(fontColorButtonListener);

            if (graphicalElement != null) {
                RGB backgroundColor = null;
                RGB borderColor = null;
                RGB fontColor = null;
                if (graphicalElement instanceof GraphNodeEditPart) {
                    backgroundColorSelector.setEnabled(true);
                    borderColorSelector.setEnabled(true);
                    backgroundColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode()
                                    .getBackgroundColor();
                    borderColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode().getBorderColor();
                    fontColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode().getFontColor();
                } else if (graphicalElement instanceof GraphEdgeEditPart) {
                    backgroundColorSelector.setEnabled(false);
                    borderColorSelector.setEnabled(false);
                    backgroundColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge()
                                    .getBackgroundColor();
                    borderColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge().getBorderColor();
                    fontColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge().getFontColor();
                } else if (graphicalElement instanceof SeDiSubpartEditPart
                        || graphicalElement instanceof AbstractLinkEditPart) {
                    if (graphicalElement.getModel() instanceof GraphElement) {
                        GraphElement graphElement = (GraphElement) graphicalElement.getModel();
                        backgroundColorSelector.setEnabled(true);
                        borderColorSelector.setEnabled(true);
                        backgroundColor = graphElement.getBackgroundColor();
                        borderColor = graphElement.getBorderColor();
                        fontColor = graphElement.getFontColor();
                    }
                }
                if (backgroundColor != null) {
                    backgroundColorSelector.setColorValue(backgroundColor);
                }
                if (borderColor != null) {
                    borderColorSelector.setColorValue(borderColor);
                }
                if (fontColor != null) {
                    fontColorSelector.setColorValue(fontColor);
                }
            }

            // Add all listeners
            backgroundColorSelector.addListener(backgroundColorButtonListener);
            borderColorSelector.addListener(borderColorButtonListener);
            fontColorSelector.addListener(fontColorButtonListener);
        }
    }
}